package com.kingyea.mobilepolice.apimanage.utils;

import com.kingyea.mobilepolice.system.exception.BusinessException;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数字工具
 *
 * @author Mr.Lin
 * @version v1.0.0
 * @description
 * @date Created in 2017/11/16 14:23
 */
// @Component
public class NumberUtils {

    /*protected static StringRedisTemplate stringRedisTemplate = ApplicationContextUtil.getBean(StringRedisTemplate.class);*/

    /**
     * 对象转Integer
     *
     * @param numObj
     * @return java.lang.Double
     * @author Mr.Lin
     * @date 2017/11/18 11:51
     */
    public static Double obj2Double(Object numObj) {
        Double result = null;
        try {
            if (numObj != null) {
                result = Double.valueOf(numObj.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR, StateCode.getMsg(StateCode.ILLEGAL_ARGUMENT_ERROR), null);
        }
        return result;
    }

    /**
     * 对象转Integer(支持正负数)
     *
     * @param numObj
     * @return java.lang.Integer
     * @author Mr.Lin
     * @date 2017/11/18 11:51
     */
    public static Integer obj2Int(Object numObj) {
        Integer result = null;
        try {
            if ((!StringUtils.isEmpty(numObj)) && (isPositiveInteger(numObj.toString()) || isNegtiveInteger(numObj.toString()))) {
                result = Integer.valueOf(numObj.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR, StateCode.getMsg(StateCode.ILLEGAL_ARGUMENT_ERROR), null);
        }
        return result;
    }

    /**
     * 是否是空
     *
     * @param numObj
     * @return java.lang.Boolean
     * @author Mr.Lin
     * @date 2017/11/18 11:53
     */
    public static Boolean isNullNumObj(Object numObj) {
        Boolean b = false;
        if (numObj == null) {
            b = true;
        }
        return b;
    }

    /**
     * 是否小于等于0
     *
     * @param numObj
     * @return java.lang.Boolean
     * @author Mr.Lin
     * @date 2017/11/18 12:34
     */
    public static Boolean isLEZeroNumObj(Object numObj) throws Exception {
        Boolean b = false;
        if (numObj != null) {
            if (numObj instanceof Integer) {
                if ((Integer) numObj <= 0) {
                    b = true;
                }
            } else if (numObj instanceof Double) {
                if ((Integer) numObj <= 0) {
                    b = true;
                }
            } else if (numObj instanceof Float) {
                if ((Float) numObj <= 0) {
                    b = true;
                }
            } else if (numObj instanceof Long) {
                if ((Long) numObj <= 0) {
                    b = true;
                }
            } else {
                return Double.valueOf(numObj.toString()) <= 0;
//                throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR,StateCode.getMsg(StateCode.ILLEGAL_ARGUMENT_ERROR),null);
            }
        }
        return b;
    }

    /**
     * 是否为空或者0  不是 数字类型 报错   注意！！！！！！
     *
     * @param numObj 必须是数字类型
     * @return java.lang.Boolean
     * @author Mr.Lin
     * @date 2017/11/18 12:35
     */
    public static Boolean isNullOrLEZero(Object numObj) throws Exception {
        return (isNullNumObj(numObj) || isLEZeroNumObj(numObj));
    }


    /**
     * 对象转 List<Integer>
     *
     * @param numListObj
     * @return java.util.List<java.lang.Integer>
     * @author Mr.Lin
     * @date 2017/11/23 11:18
     */
    public static List<Integer> obj2IntList(Object numListObj) throws Exception {
        List objectList = null;
        if (numListObj instanceof List) {
            objectList = (List) numListObj;
        } else {
            throw new BusinessException(StateCode.ILLEGAL_ARGUMENT_ERROR, StateCode.getMsg(StateCode.ILLEGAL_ARGUMENT_ERROR), null);
        }
        List<Integer> intList = new ArrayList<>();
        for (Object o : objectList) {
            intList.add(obj2Int(o));
        }
        return intList;
    }

    /**
     * 获取指定长度的uuid，目前用在orderNo字段
     *
     * @param length
     * @return java.lang.String
     * @author Mr.Lin
     * @date 2017/11/24 10:48
     */
    public static String getSeriesNo(Integer length) throws Exception {
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        return uuidStr.substring(0, isNullOrLEZero(length) ? uuidStr.length() : length).toUpperCase();
    }

    public static void main(String[] args) throws Exception {
        /*System.out.println(getSeriesNo(11));
        System.out.println(Double.valueOf("-0.1"));
        System.out.println(Double.valueOf("-0.1")>0);*/
        System.out.println(getRandomIntegerByLength(6));
        System.out.println(obj2Int(2));
        System.out.println(obj2Int("2"));
        System.out.println(obj2Int("\"2\""));
        System.out.println(("\"2\""));
        System.out.println(obj2Int("asd"));
        System.out.println(obj2Int(null));

    }

    /**
     * 校验是否为正整数
     *
     * @param str
     * @return boolean
     * @author Mr.Lin
     * @date 2017/12/14 18:09
     */
    public static boolean isPositiveInteger(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 校验是否为负整数
     *
     * @param str
     * @return boolean
     * @author Mr.Lin
     * @date 2017/12/14 18:09
     */
    public static boolean isNegtiveInteger(String str) {
        Pattern pattern = Pattern.compile("^-?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 校验是否数字
     *
     * @param str
     * @return boolean
     * @author Mr.Lin
     * @date 2017/12/14 18:09
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }


    /**
     * 获取一定长度的随机数字验证码
     *
     * @param length
     * @return java.lang.String
     * @author Mr.Lin
     * @date 2018/1/9 10:13
     */
    public static String getRandomIntegerByLength(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}